#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import os
import argparse
import numpy.matlib
from utils.search import hierar_search_file

parser = argparse.ArgumentParser()
parser.add_argument(
    '-f'
    ,type=str
    ,help='path to rsf filr.'
    ,required=True
)
args = parser.parse_args()

args.f = hierar_search_file(args.f,cwd=os.getcwd())
# print(args.f)
# exit(1)

with open(args.f,'r') as f:
    lines = f.readlines()
    nodes = set()
    node2idx = {}
    idx2node = {}
    edges = {}
    for line in lines:
        tokens = line.split()
        if tokens[1] not in nodes:
            nodes.add(tokens[1])
            node2idx[tokens[1]]  = len(nodes)
            idx2node[len(nodes)] = tokens[1]
        if tokens[2] not in nodes:
            nodes.add(tokens[2])
            node2idx[tokens[2]]  = len(nodes)
            idx2node[len(nodes)] = tokens[2]
        edges[node2idx[tokens[1]]]=node2idx[tokens[2]]

    with open('idx2node.txt','w') as idx2nodef:
        for i in range(1,len(nodes)+1):
            print(i," ",idx2node[i],file=idx2nodef)
    # # 打印节点标号        
    # for i in range(1,len(nodes)+1):
    #     print(i," ",idx2node[i])
    # # 打印边
    # for i in edges.items():
    #     print(i)
    
    with open('nodes.txt','w') as nodesf:
        n = len(nodes)
        fca_input_matrix = numpy.matlib.zeros((n,n),dtype=int)
        for i in edges.items():
            fca_input_matrix[i[0]-1,i[1]-1] = 1

        for i in range(0,n):
            for j in range(0,n):
                print(fca_input_matrix[i,j],' ',end='',file=nodesf)
            print(';',file=nodesf)

    # with open('nodes_undirect.txt','w') as nodesf:
    #     n = len(nodes)
    #     fca_input_matrix = numpy.matlib.zeros((n,n),dtype=int)
    #     for i in edges.items():
    #         fca_input_matrix[i[0]-1,i[1]-1] = 1
    #         fca_input_matrix[i[1]-1,i[0]-1] = 1

    #     for i in range(0,n):
    #         for j in range(0,n):
    #             print(fca_input_matrix[i,j],' ',end='',file=nodesf)
    #         print(';',file=nodesf)